package com.dzt.btserial;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.adayo.btsppsdk.utils.BluetoothKit;

import java.util.List;

/**
 * Created by M02323 on 2017/8/2.
 */

public class BtListAdapter extends BaseAdapter {

	private List<BluetoothDevice> list;
	private LayoutInflater inflater;

	public BtListAdapter(Context context, List<BluetoothDevice> list) {
		this.list = list;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int i) {
		return list.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.bt_item, viewGroup, false);
			holder.tvName = (TextView) view.findViewById(R.id.tv_name);
			holder.tvAddress = (TextView) view.findViewById(R.id.tv_address);
			holder.tvStatus = (TextView) view.findViewById(R.id.tv_status);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		BluetoothDevice device = list.get(i);
		BluetoothDevice remote = BluetoothKit.getInstance().getRemoteDevice(device.getAddress());
		holder.tvName.setText(remote.getName());
		holder.tvAddress.setText(remote.getAddress());
		if (remote.getBondState() == BluetoothDevice.BOND_BONDED) {
			holder.tvStatus.setText("已配对");
		} else {
			holder.tvStatus.setText("未配对");
		}
		return view;
	}

	class ViewHolder {
		public TextView tvName;
		public TextView tvAddress;
		public TextView tvStatus;
	}
}
